package model.workersWithData;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;

import model.workerwithfile.ReaderFromFile;
import model.workerwithfile.WriterInFile;
import autoproperties.AutoProperties;
import entities.DataBase;
import entities.IGarage;
import entities.IMaster;
import entities.IOrder;
import entities.OrderStatus;
import exportimporterentities.ExportImportEntities;

public class DBManager {

	private ArrayList<IOrder> orderList = new ArrayList<IOrder>();
	private ArrayList<IMaster> masterList = new ArrayList<IMaster>();
	private ArrayList<IGarage> garageList = new ArrayList<IGarage>();
	private DataBase db;
	private ExportImportEntities export_import_entities = new ExportImportEntities();
	private AutoProperties auto_properties = AutoProperties.getInstance();
	private static final String THIS_ACTION_NOT_ALLOWED = "Sorry, this action is not allowed";

	public DBManager() {
		db = ReaderFromFile.getDBFromFile();
		masterList.addAll(db.getMasterList());
		garageList.addAll(db.getGarageList());
		orderList.addAll(db.getOrderList());
	}

	public Integer getEmptyGarage() {
		return WorkerWithGarage.getEmptyGarage(garageList);
	}

	public IOrder getOrdersByMaster(String masterName) {
		return WorkerWithMaster.getOrdersByMaster(masterName, masterList);
	}

	public IMaster getMastersByOrder(Integer numberOrder) {
		return WorkerWithOrder.getMastersByOrder(numberOrder, orderList);
	}

	public Integer getNumberEmptyPlaces(Date date) {
		return WorkerWithGarage.getNumberEmptyPlaces(masterList, garageList,
				date);
	}

	public String getNextFreeDate() {
		return WorkerWithOrder.getNextFreeDate(orderList);
	}

	public ArrayList<IOrder> SortOrder(Comparator sortBy) {
		return WorkerWithOrder.SortOrder(orderList, sortBy);
	}

	public ArrayList<IMaster> SortMaster(Comparator sortBy) {
		return WorkerWithMaster.SortMaster(masterList, sortBy);
	}

	public ArrayList<IOrder> SortMadingOrder(Comparator sortBy) {
		return WorkerWithOrder.getListMadingOrder(orderList, sortBy);

	}

	public ArrayList<IOrder> SortOrderByPeriod(OrderStatus ordSt,
			Comparator sortBy, Date begin, Date end) {
		return WorkerWithOrder.getOrderByPeriod(orderList, sortBy, ordSt,
				begin, end);
	}

	public void addMaster(IMaster master) {
		WorkerWithMaster.addMaster(masterList, master);
	}

	public void deleteMaster(String masterName) {
		WorkerWithMaster.deleteMaster(masterList, masterName);
	}

	public void addGarage(IGarage garage) {
		if (auto_properties.getGarageManager() == true) {
			WorkerWithGarage.addGarage(garageList, garage);
		} else {
			throw new RuntimeException(THIS_ACTION_NOT_ALLOWED);
		}

	}

	public void deleteGarage(Integer numberGarage) {
		if (auto_properties.getGarageManager()) {
			WorkerWithGarage.deleteGarage(garageList, numberGarage);
		} else {
			throw new RuntimeException(THIS_ACTION_NOT_ALLOWED);
		}

	}

	public void addOrder(IOrder order, String masterName, Integer numberGarage) {
		WorkerWithOrder.addOrder(orderList, order, masterList, garageList,
				masterName, numberGarage);
	}

	public void markOrderDeleted(Integer numberOrder) {
		if (auto_properties.getPermissionDeleteOrder() == true) {
			WorkerWithOrder.markOrder(orderList, OrderStatus.DELETED,
					numberOrder);
		} else {
			throw new RuntimeException(THIS_ACTION_NOT_ALLOWED);
		}

	}

	public void markOrderCanceled(Integer numberOrder) {
		WorkerWithOrder.markOrder(orderList, OrderStatus.CANCELED, numberOrder);
	}

	public void markOrderMade(Integer numberOrder) {
		WorkerWithOrder.markOrder(orderList, OrderStatus.MADE, numberOrder);
	}

	public void ShirtTimeOrder(Date newDate, Integer numberOrder) {
		if (auto_properties.getPermissionShiftTimeOrder()) {
			WorkerWithOrder.ShirtTimeOrder(orderList, newDate, numberOrder);
		} else {
			throw new RuntimeException(THIS_ACTION_NOT_ALLOWED);
		}

	}

	public IOrder cloneOrder(Integer numberOrder) {
		IOrder clone = null;
		for (IOrder o : orderList) {
			if (o.getNumberOrder() == numberOrder) {
				clone = WorkerWithOrder.cloneOrder(o);
			}
		}
		return clone;
	}

	public void exportGarages() {
		export_import_entities.ExportGarages(garageList);

	}

	public void exportMasters() {
		export_import_entities.ExportMasters(masterList);

	}

	public void exportOrders() {
		export_import_entities.ExportOrders(orderList);
	}

	public void importGarages() {
		export_import_entities.ImportGarages(garageList);
	}

	public void importMasters() {
		export_import_entities.ImportMasters(masterList);
	}

	public void importOrders() {
		export_import_entities.ImportOrders(orderList);
	}

	public void saveData() {
		db.saveGarageListToBD(garageList);
		db.saveMasterListToBD(masterList);
		db.saveOrderListToBD(orderList);
		WriterInFile.saveData(db);
	}

}
